<?php
class BlockModel extends Model {
	
	var $tableName = 'cms_block';

	/* 返回内容ID查询所属区块
	 * M2
	 * @param int $contentID
	 * @return array
	 */
	public function reBlockListByContentID($contentID) {
		$arr = array();
		$list = $this->getBlockListByContentID($contentID);

		foreach ($list as $k => $v) {
			$arr[$k]['id'] = $v['id'];
			$arr[$k]['name'] = $v['name'];
		}
		return $arr;
	}

	/* 根据内容ID删除所属区块
	 * M2
	 * @param int $contentID
	 * @return boolean
	 */
	public function delBlockListByContentID($id, $contentID) {
		$list = $this->getBlockListByContentID($contentID);

		foreach ($list as $k => $v) {
			$blockid = intval($v['id']);
			if ($blockid == $id) {
				$oldarr = explode(',',$v['content_number']);
	            if (in_array($contentID, $oldarr)) {
	                $unset_key = array_search($contentID,$oldarr);
	                unset($oldarr[$unset_key]);
	            }
	            $newarr = array_unique($oldarr);

	            $data['content_number'] = implode(',', $newarr);
				
				$this->where('id='.$blockid)->save($data);
			}
		}
		return true;
	}

	/* 根据内容ID获取所属区块
	 * M2
	 * @param int $contentID
	 * @return boolean || array
	 */
	private function getBlockListByContentID($contentID) {
		if (!$contentID) {
			return false;
		}
		
		$data['content_number'] = array('like', '%'.$contentID.'%');
		$list = $this->where($data)->select();
		return $list;
	}
}
?>